package com.tongzhan.service;

import com.tongzhan.base.BaseCriteria;
import com.tongzhan.base.BaseShareService;
import com.tongzhan.entity.MarkPrompt;
import com.tongzhan.entity.party.Discuss;
import com.tongzhan.entity.party.Participation;
import com.tongzhan.entity.party.Reply;
import com.tongzhan.utils.ThreadLocalUtil;
import com.tongzhan.vo.DiscussVo;
import com.tongzhan.vo.ParticipationVo;
import com.tongzhan.vo.ReplyVo;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author htmic
 * @date 2020/7/29
 */
@Service
public class SocailService extends BaseShareService {

    public long addParticipation(Participation participation) {
        participation.setIs_del(0);
        participation.setType(1);
        participation.setStreetCode(ThreadLocalUtil.getThreadLocal().getStreetCode());
        participation.setVillageCode(ThreadLocalUtil.getThreadLocal().getVillageCode());
        long l = participationRepository.insertSelective(participation);
        if (l > 0) {
            //添加角标
            markPromptService.add(new MarkPrompt(2, participation.getPid()),"12");
            return 1;
        }
        return 0;
    }


    public ParticipationVo participationDetail(String pid,String userId) {
        ParticipationVo participationVo = new ParticipationVo();
        Participation participation = participationRepository.findByPrimaryKey(pid);
        if (participation == null) {
            return null;
        }
        BeanUtils.copyProperties(participation, participationVo);
        participationVo.setContent(participation.getParticipation_content());
        participationVo.setUrl(participation.getAction_picture());
        participationVo.setTitle(participation.getTitle());
        Query query = new Query();
        List<Discuss> discussList = discussRepository.find(query.addCriteria(BaseCriteria.where("is_del").is(0).and("discuss_comeid").is(pid)), null);
        List<DiscussVo> dlist = new ArrayList<>();
        discussList.forEach(c -> {
            List<ReplyVo> rlist = new ArrayList<>();

            DiscussVo discussVo = new DiscussVo();
            discussVo.setContent(c.getDiscuss_content());
            discussVo.setDid(c.getDid());
            discussVo.setUsername(c.getUsername());
            discussVo.setTime(c.getDiscuss_time());
            discussVo.setDiscuss_pic(c.getDiscuss_pic());
            List<Reply> replyList = replyRepository.find(new Query().addCriteria(BaseCriteria.where("is_del").is(0).and("did").is(c.getDid())), null);
            replyList.forEach(r -> {
                ReplyVo replyVo = new ReplyVo();
                replyVo.setContent(r.getReply_content());
                replyVo.setRid(r.getRid());
                replyVo.setUsername(r.getReplyName());
                replyVo.setTime(r.getReply_time());
                rlist.add(replyVo);
            });
            discussVo.setReplyList(rlist);
            dlist.add(discussVo);
        });
        participationVo.setDiscussList(dlist);

        //标记角标已读
        markPromptService.read(pid,userId);
        return participationVo;
    }

    public Page<Participation> participationList(Integer page, Integer size) {
        Query query = new Query(BaseCriteria.where("is_del").is(0).and("type").is(1));
        if (ThreadLocalUtil.getThreadLocal().getDutiesCode().equals(ThreadLocalUtil.STREET_CODE)) {
            query.addCriteria(BaseCriteria.where("streetCode").like(ThreadLocalUtil.getThreadLocal().getStreetCode()));
        }
        if (ThreadLocalUtil.getThreadLocal().getDutiesCode().equals(ThreadLocalUtil.VILLAGE_CODE)) {
            query.addCriteria(BaseCriteria.where("villageCode").like(ThreadLocalUtil.getThreadLocal().getVillageCode()));
        }
        PageRequest of = PageRequest.of(page, size, Sort.Direction.DESC, "time");
        Page<Participation> all = participationRepository.findPage(query, of);
        return all;
    }

    public long adddiscuss(Discuss discuss) {
        discuss.setDiscuss_time(new Date());
        discuss.setIs_del(0);
        discuss.setType(1);
        long l = discussRepository.insertSelective(discuss);
        if (l > 0) {
            //添加角标
            markPromptService.add(new MarkPrompt(2, discuss.getDiscuss_comeid()),"11");
            return 1;
        }
        return 0;
    }

    public long addreply(Reply reply) {
        reply.setIs_del(0);
        reply.setType(1);
        reply.setReply_time(new Date());
        //查询评论数据
        Discuss discuss = discussRepository.findByPrimaryKey(reply.getDid());
        if(discuss!=null) {
            long l = replyRepository.insertSelective(reply);
            if (l > 0) {
                //添加角标
                markPromptService.add(new MarkPrompt(2, discuss.getDiscuss_comeid()),"11");
                return 1;
            }
        }
        return 0;
    }

    public long delreply(String rid) {
        Reply reply = new Reply();
        reply.setRid(rid);
        reply.setIs_del(1);
        long l = replyRepository.updateByPrimaryKeySelective(reply);
        return l;
    }

    public long deldiscuss(String did) {
        Discuss discuss = new Discuss();
        discuss.setIs_del(1);
        discuss.setDid(did);
        long l = discussRepository.updateByPrimaryKeySelective(discuss);
        return l;
    }
}
